package com.student.education.controller;

import com.student.education.common.CommonResult;
import com.student.education.entity.Subject;
import com.student.education.service.SubjectService;
import com.student.education.to.SubjectTo;
import com.student.education.vo.SubjectVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectService subjectService;

    //查询 这个 应该是list 且带分页
    @PostMapping("/queryAll")
    public CommonResult<List<Subject>> queryAll(@RequestBody SubjectVo Vo) {    //Vo是入参，只有name和description

        List<Subject> subjectList = subjectService.queryAll(Vo);
        return CommonResult.success(subjectList);
    }

    @PostMapping("add")
    /*可以添加成功，但可以重复添加*/
    public CommonResult<String> add(@RequestBody SubjectVo Vo) {

        int i = subjectService.add(Vo);

        if (Objects.equals(i, 1)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.error("添加失败");
    }

    //修改
    @PostMapping("update")
    public CommonResult<String> update(@RequestBody Subject subject) {
        int i = subjectService.update(subject);
        if (Objects.equals(i, 1)) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.error("修改失败");
    }

    //删除
    @GetMapping("delete")
    public CommonResult<String> delete(@RequestParam Integer id) {
        int i = subjectService.delete(id);
        if (Objects.equals(i, 1)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.error("删除失败");
    }


}
